//
//  LookController.m
//  MyTrip
//
//  Created by zhang on 15/8/26.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "LookController.h"
#import "HMSegmentedControl.h"
#import "AsyncGETConnection.h"
#import "TypeCell.h"
#import "LookTypeHeaderView.h"
#import "PackDetailController.h"
#import "MBProgressHUD.h"

@interface LookController ()<UICollectionViewDataSource, UICollectionViewDelegate, TypeCellDaelegate>

@property (nonatomic, retain) HMSegmentedControl *segmentedControl;
@property (nonatomic, retain) NSMutableArray *typeArray;
@property (nonatomic, retain) UICollectionView *lookCollection;
@property (nonatomic, retain) NSMutableArray *idArr;
@property (nonatomic, retain) NSMutableArray *indexOneArray;
@property (nonatomic, retain) NSMutableArray *indexTwoArray;
@property (nonatomic, retain) NSMutableArray *indexThreeArray;
@property (nonatomic, retain) NSMutableArray *indexFourArray;
@property (nonatomic, retain) NSMutableArray *indexFiveArray;
@property (nonatomic, retain) NSMutableArray *indexSixArray;
@property (nonatomic, retain) NSMutableArray *indexSevenArray;
@property (nonatomic, retain) NSMutableArray *indexEightArray;
@property (nonatomic, retain) NSMutableArray *indexNineArray;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation LookController

- (void)dealloc
{
    [_idArr release];
    [_typeArray release];
    [_segmentedControl release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.typeArray = [NSMutableArray array];
        self.idArr = [NSMutableArray array];
        self.indexOneArray = [NSMutableArray array];
    }
    return self;
}

- (void)getMessage{
    [AsyncGETConnection asyncGETConnectionWithURLStr:@"http://open.qyer.com/qyer/guide/category_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C1&track_deviceid=216B2645-C28F-42CC-A74E-17449CA8DF1A&track_os=ios%25208.1.3&v=1" parameters:nil messageBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *typeArray = [dic objectForKey:@"data"];
        [_typeArray addObjectsFromArray:typeArray];
        for (int i = 0; i < _typeArray.count; i++) {
            NSDictionary *type = [_typeArray objectAtIndex:i];
            NSNumber *idNum = [type objectForKey:@"id"];
            [_idArr addObject:idNum];
        }
        [_lookCollection reloadData];
    }];
    
    
    
}


- (void)createSegmentedControl{
    
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 64 * OffHeight, self.view.frame.size.width, 40 * OffHeight)];
    aView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: aView];
    [aView release];
    
    self.segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(10 * OffWidth, 0, 375 * OffWidth, 40 * OffHeight)];
    self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    _segmentedControl.sectionTitles = @[@"最新", @"亚洲", @"欧洲", @"北美洲" , @"南美洲", @"大洋洲" , @"非洲" , @"专题锦囊", @"中国内地"];
    self.segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10 * OffWidth, 0, 20 * OffWidth);
    [self.segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor grayColor]}];
        return attString;
    }];
    [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [aView addSubview:_segmentedControl];
    
}


- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
    
    NSLog(@"Selected index = %ld", (long)segmentedControl.selectedSegmentIndex);
}

#pragma mark --- segmentControl 控制下面collectionView 的偏移量
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    
    NSLog(@"Selected index = %ld ", (long)segmentedControl.selectedSegmentIndex);
    
    self.lookCollection.contentOffset = CGPointMake(segmentedControl.selectedSegmentIndex * self.view.frame.size.width, 0);
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat pageWidth = self.view.frame.size.width;
    NSInteger page = self.lookCollection.contentOffset.x / pageWidth;
    
    [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
}

- (void)createLookCollection{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 49 * OffHeight - 100 * OffHeight);
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0.1, 0, 0.1);
    self.lookCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40 + 64, self.view.frame.size.width, self.view.frame.size.height - 49 * OffHeight - 100 * OffHeight) collectionViewLayout:flowLayout];
    self.lookCollection.pagingEnabled = YES;
    self.lookCollection.dataSource = self;
    self.lookCollection.delegate = self;
    self.lookCollection.backgroundColor = [UIColor whiteColor];
    
#pragma mark-- 偏移量
    self.lookCollection.contentSize = CGSizeMake(self.view.frame.size.width * 10, 0);
    self.lookCollection.contentOffset = CGPointMake(0, 0);
    
    self.lookCollection.pagingEnabled = YES;
    
    [self.lookCollection registerClass:[TypeCell class] forCellWithReuseIdentifier:@"typecell"];
   
    [self.view addSubview:_lookCollection];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _typeArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"typecell" forIndexPath:indexPath];
    NSInteger idNum = [[_idArr objectAtIndex:indexPath.item] integerValue];
    NSNumber *num = [NSNumber numberWithInteger:idNum];
    NSDictionary *urlid = [NSDictionary dictionaryWithObjectsAndKeys:num, @"id", nil];
    NSLog(@"%@", urlid);
    self.hud = [[MBProgressHUD alloc] init];
    [self.view addSubview:_hud];
    [_hud show:YES];
    _hud.labelText = @"正在拼命加载......";
    //    在网络请求之后
    
    __block MBProgressHUD *hud = _hud;
    [AsyncGETConnection asyncGETConnectionWithURLStr:@"http://open.qyer.com/qyer/guide/channel_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C1&track_deviceid=216B2645-C28F-42CC-A74E-17449CA8DF1A&track_os=ios%25208.1.3&v=1" parameters:urlid messageBlock:^(NSData *data) {
        _hud.hidden = YES;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"data"];
        cell.delegate = self;
        cell.typeArray = array;
        [cell.typeCollection reloadData];
    }];
    
    
    return cell;
}

- (void)clickIndex:(NSIndexPath *)indexPath WithIdNum:(NSInteger)idNum{
    
    
    PackDetailController *pack = [[PackDetailController alloc] init];
    pack.idNum = idNum;
    [self.navigationController pushViewController:pack animated:YES];
    [pack release];
    
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.tabBarController.tabBar.hidden = YES;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1];
    
    self.navigationItem.title = @"锦囊在手";
    [self createSegmentedControl];
    [self createLookCollection];
    [self getMessage];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
